//
//  HomeTableViewController.swift
//  weiboFirst
//
//  Created by ZhouRobert on 15/6/24.
//  Copyright © 2015年 ZhouRobert. All rights reserved.
//

import UIKit
import SVProgressHUD

class HomeTableViewController: BaseTableViewController, StatusCellDelegate {
    
    var statusesList: [Status]? {
        didSet {
            
            //刷新表格
            tableView.reloadData()
        }
        
    }
    
    //定义行高缓存[statusId:行高］
    lazy var rowHeightCache = [Int: CGFloat]()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        visitorView?.setupViewInfo("visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜", isHome: true)
        
        if  !userLogon {

            return
}
        //以下代码都是用户登录后才需要执行的
        setupNavigationBar()
        
        prepareTableView()
        
        prepareRefresh()

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        //清空缓存，提示： NSCache 不能清空
        rowHeightCache.removeAll()
    }
    
    ///  加载微博数据
 func loadData() {
    //开始刷新－－不会触发监听方法，会显示刷新控件
     refreshControl?.beginRefreshing()
    
    //下拉刷新的代号， 取到数组中的第1条数据的id
    var since_id = statusesList?.first?.id ?? 0
    
    //上拉刷新，取到数组中最后1条数据的id
    
    var max_id = 0
    
    if pullRefreshFlag {
        
        since_id = 0
        
        max_id = statusesList?.last?.id ?? 0
    }
    

    
    
     Status.loadStatus(since_id, max_id: max_id) { (statuses, error) -> () in
        
        //关闭刷新控件
        self.refreshControl?.endRefreshing()
        
            if error != nil {
                //不要把错误的信息告诉用户，程序员应该所测试
                SVProgressHUD.showInfoWithStatus("您的网络不给力")
                return
            }
            
            //提示，后续会有下拉刷新，没有心微博的情况，不报错
            if statuses == nil {
                print("没有数据")
                return
            }
            
            //跟踪转发微博
            print ("加载了 \(statuses?.count) 条微博")
            
            //判断是否下啦刷新，将新的数据，添加到原有数组的前面
            
            if since_id > 0 {
                
                self.statusesList = statuses! + self.statusesList!
                
            } else if max_id > 0 {
                //上拉刷新
                self.statusesList = self.statusesList! + statuses!
                
                self.pullRefreshFlag = false

            } else {
                //初始刷新
                self.statusesList = statuses
                
           }
            
        }
        
    }
    
    
    
    override func viewDidAppear(animated: Bool) {
       super.viewDidAppear(animated)
        
        //开启动画
        visitorView?.startAnimation()
    }
    
    //MARK: - 表格的数据源方法
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusesList?.count ?? 0
        
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //1. 取得数据模型
        let status = statusesList![indexPath.row]
        
        //2. 取表示符
        let ID = StatusCell.cellIdentifier(status)
        // 此方法必须注册可重用cell，如果不注册，会崩溃
        let cell = tableView.dequeueReusableCellWithIdentifier(ID, forIndexPath: indexPath) as! StatusCell
        
        
        //设置代理
        cell.photoDelegate = self
        
        //设置 cell
        
        cell.status = status
        
        //判断是否是最后一行
        if indexPath.row == statusesList!.count - 1{
            
            pullRefreshFlag = true
            loadData()
        }
        
        
        return cell
        
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        //1. 取得对象
        let status = statusesList![indexPath.row]
        
        //1.1 判断是否缓存了行高，如果有，直接返回
        if rowHeightCache[status.id] != nil {
            
            return rowHeightCache[status.id]!
        }
        
        //2. 获取 cell, 从可重用的 缓冲池 中提取一个cell，说明之前用过，要计算行高准确，需要实例化一个cell,否则会造成行高不准确
        // Xcode 7.0 新发现的
        //let cell = tableView.dequeueReusableCellWithIdentifier(StatusCell.cellIdentifier(status)) as! StatusCell
        
        //2.1 获取可重用的ID
        let ID = StatusCell.cellIdentifier(status)
        //2.2 定义一个类，默认是原创微博的cell
        var cls: AnyClass = StatusNormalCell.self
        //2.3
        if ID == IBStatusForwardCellIdentifier {
            
            cls = StatusForwardCell.self
        }
        //2.4 根据cls创建cell
        let cell = cls.new() as! StatusCell
        
        //3. 返回行高
        let height = cell.rowHeight(status)
       
        rowHeightCache[status.id] = height
        
        return height
    }
    
    //MARK: - cell 的代理方法
    func statusCellDidSelectedPhoto(cell: StatusCell, photoIndex: Int) {
        
        if cell.status?.largePicURLs == nil {
            return
        }
        
        //传递数据！－图像的数组 & 用户选中的照片索引
        let vc = PhotoBrowserViewController(urls: cell.status!.largePicURLs!, index: photoIndex)
        
        presentViewController(vc, animated: true, completion: nil)
    }
    
 /// 显示二维码界面
    func showQRCode() {
        
        let sb = UIStoryboard(name: "QRCode", bundle: nil)
        presentViewController(sb.instantiateInitialViewController()!, animated: true, completion: nil)
        
    }
    
    
    //动画转场代理
    let popoverAnimator = PopoverAnimator()

    func titleButtonClicked() {
        let sb = UIStoryboard(name: "FriendGroup", bundle:nil)
        let vc = sb.instantiateViewControllerWithIdentifier("FriendGroupSB")
        
        //1. 设置“转场”代理
        vc.transitioningDelegate = popoverAnimator
        
        // 2. 设置视图的展现大小
        
        let x = (view.bounds.width - 200) * 0.5
        popoverAnimator.presentFrame = CGRectMake(x, 56, 200, 300)
        // 3. 设置专场的模式 — 自定义转场动画
        vc.modalPresentationStyle = UIModalPresentationStyle.Custom
        
        presentViewController(vc, animated: true, completion: nil)
        
    }
    
    //MARK: -设置UI
    
///  设置导航条
    private func setupNavigationBar() {
        //判断用户是否登录
        if sharedUserAccount == nil {
            return
        }
        //1. 设置左侧按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", highlightedImageName: nil)
        // 设置右侧按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", highlightedImageName:nil)
        
        print(navigationItem.rightBarButtonItem?.customView)
        //1.1 获取button
        let btn = navigationItem.rightBarButtonItem!.customView as! UIButton
        
        //1.2 添加target
        btn.addTarget(self, action: "showQRCode", forControlEvents: UIControlEvents.TouchUpInside)

        
        // 2. 设置标题
        
        let titleButton = HomeTitleButton.button(sharedUserAccount!.name!)
       
        titleButton.addTarget(self, action: "titleButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        
        navigationItem.titleView = titleButton
    }
    
///  准备 tableView
    private func prepareTableView() {
        
        //注册可重用cell
        tableView.registerClass(StatusForwardCell.self, forCellReuseIdentifier: IBStatusForwardCellIdentifier)
        tableView.registerClass(StatusNormalCell.self, forCellReuseIdentifier: IBStatusNormalCellIdentifier)
        
        //取消分割线
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        
    }
    
///  准备刷新控件
    private func prepareRefresh() {
        refreshControl = IBRefreshControl()
        
        //添加监听方法
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
        
        //加载数据
        loadData()
        
        
    }
    
    //上拉刷新标记
    private var pullRefreshFlag = false 
}


